﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HtmlFormatApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 

        private void html格式化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string source = myTextBox.Text.Trim();
            source = HtmlCodeFormat.Format(source);
            myTextBox.Text = source;
        }

        private void html转化成字符串ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string source = myTextBox.Text.Trim();
            source = HtmlCodeFormat.Format(source);
            var lins = source.Split('\n');
            StringBuilder sb = new StringBuilder();
            foreach (var line in lins)
            {
                if (line.Length == 0||line.Trim().Length==0)
                {
                    continue;
                }
                string tem = line;
                char c = line[line.Length - 1];
                if (c == '\r')
                {
                    tem = line.Substring(0, line.Length - 1);
                }
                tem = tem.Trim();
                if (tem.Length == 0) {
                    continue;
                }
                sb.Append(tem+"\\"+Environment.NewLine);
            }
            myTextBox.Text = "var html='"+sb.ToString()+"'";
        }

        private void html压缩ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string source = myTextBox.Text.Trim();
            var lins = source.Split('\n');
            StringBuilder sb = new StringBuilder();
            foreach (var line in lins)
            {
                if (line.Length == 0)
                {
                    continue;
                }
                string tem = line;
                char c = line[line.Length - 1];
                if (c == '\r')
                {
                    tem = line.Substring(0, line.Length - 1);
                }
                tem = tem.Trim();
                sb.Append(tem);
            }
            myTextBox.Text = sb.ToString();
        }

        private void json转义ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string source = myTextBox.Text.Trim();
            source = StringToUniCode.JsonToUnicode(source);
            myTextBox.Text = source;
        }

        private void pomteplateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK) {
                DirectoryInfo dinfo = new DirectoryInfo(fbd.SelectedPath);
                var files = dinfo.GetFiles("pom.xml.template", SearchOption.AllDirectories);
                List<string> list = new List<string>();
                foreach (var item in files)
                {
                    try
                    {
                        var source = item.FullName;
                        var target = source.Substring(0, source.Length - 9);
                        File.Move(source, target);
                    }
                    catch (Exception Err)
                    {

                        list.Add(Err.ToString());
                    }
                }
                myTextBox.ForeColor = Color.Red;
                if (list.Count == 0){
                    myTextBox.Text = "操作成功";
                }
                else {
                    string error = string.Join(Environment.NewLine, list.ToArray());
                    myTextBox.Text = error;
                }
            }
        }
    }
}
